using System;

using System.Reflection;
using System.ComponentModel;
using CUtil;
using System.Diagnostics;
using System.Collections.Generic;
using System.Collections;


namespace CStock
{
	
	/// <summary>
	/// Summary description for StockObject.
	/// </summary>
    public class StockObjectCollection : StockObjectBase, IStockObjectCollection
	{
		

		//[System.Xml.Serialization.XmlIgnoreAttribute()]
		
		[System.Xml.Serialization.XmlIgnoreAttribute()]
		protected System.Collections.ArrayList items;

		

		public StockObjectCollection(IStockObjectBase myParent)
		{
			this.parent = myParent;
			//keep empty constructor - the overriden constructor sets the collection:
			this.items = new System.Collections.ArrayList();
		}
        public StockObjectCollection()
		{
			//keep empty constructor - the overriden constructor sets the collection:
			this.items = new System.Collections.ArrayList();
		}

	
	
		[System.Xml.Serialization.XmlIgnoreAttribute()]
		public virtual System.Collections.ArrayList Items
		{
			get
			{
				return this.items;
			}
			set
			{
				this.items = value;
			}
		}
		public void Sort()
        {
            this.items = StockObjectBase.SortObjects(this.items);

        
        }
        public StockObjectBase[] ToArray()
        {
            List<StockObjectBase> list = new List<StockObjectBase>();
            foreach (StockObjectBase value in this.items)
            {
                list.Add(value);
            }
            return list.ToArray();
        }

		[System.Xml.Serialization.XmlIgnoreAttribute()]
		public virtual object this [int index]
		{
			get 
			{
				if (this.items.Count > index) 
					//return (double) this.Items[index]; 
					return (object) this.items[index]; 
				else
					return 0;
			}
			set 
			{ 
				if (this.items.Count > index) 
					this.items[index] = value ; 
			}
		}
        [System.Xml.Serialization.XmlIgnoreAttribute()]
        public virtual object this[string strID]
        {
            get
            {
                for (int i = 0; i < this.items.Count; i++)
                {
                    StockObjectBase sob = this.items[i] as StockObjectBase;
                    if (sob.ID == strID)
                        return sob;
                    //return null;
                }
                return null;
            }
           
        }

        public static IList<IStockObjectBase> SortList(IList<IStockObjectBase> list)
        {
            try
            {
                SortedList sortedList = new SortedList();
                for (int i = 0; i < list.Count; i++)
                {
                    IStockObjectBase c = list[i];
                    if (!sortedList.ContainsValue(c))
                        sortedList.Add(c.ID, c);
                }
                list = new List<IStockObjectBase>();
                for (int i = 0; i < sortedList.Count; i++)
                {
                    IStockObjectBase c = sortedList.GetByIndex(i) as IStockObjectBase;
                    list.Add(c);
                }

            }
            catch (Exception exc)
            {
                Debug.Assert(false, "Error sorting array list", exc.Message);

            }
            return list;

        }
  
	}
	


}
